﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperJoe.Framework.UnitOfWorkSql;
using WWX.Admin.IDAL;
using WWX.Model;

namespace WWX.Admin.DAL
{
    public class UserDAL : BaseDAL, IUserDAL
    {
        public UserDAL(IUnitOfWork uow) : base(uow)
        {
        }

        public void Add(User user)
        {
            var sql = "insert into users(id,account,pwd,remark, createdtime,createdby,updatedtime,updatedby,isactive)values(?id,?account,?pwd,?remark, ?createdtime,?createdby,?updatedtime,?updatedby,1)";

            var parameters = new Dictionary<string, object>();
            parameters.Add("id", user.Id);
            parameters.Add("account", user.Account);
            parameters.Add("pwd", user.Pwd);
            parameters.Add("remark", user.Remark);
            parameters.Add("createdtime", user.CreatedTime);
            parameters.Add("createdby", user.CreatedBy);
            parameters.Add("updatedtime", user.UpdatedTime);
            parameters.Add("updatedby", user.UpdateBy);
            this.Execute(sql, parameters);
        }

        public User Get(string id)
        {
            var sql = "select * from users where id=?id";
            return this.GetModel<User>(sql, new Dictionary<string, object>() { { "id", id } });
        }

        public User Get(string account, string pwd)
        {
            var sql = "select * from users where account=?account and pwd=?pwd";
            var parameters = new Dictionary<string, object>() {
                { "account",account},
                { "pwd",pwd}
            };
            return this.GetModel<User>(sql, parameters);
        }

        public List<User> GetList()
        {
            var sql = "select * from users";
            return this.GetModels<User>(sql);
        }

        public void Update(User user)
        {
            var sql = "update users set remark = ?remark, pwd=?pwd, isactive=?isactive where id=?id";
            var parameters = new Dictionary<string, object>();
            parameters.Add("remark", user.Remark);
            parameters.Add("pwd", user.Pwd);
            parameters.Add("isactive", user.IsActive);
            parameters.Add("id", user.Id);
            this.Execute(sql, parameters);
        }
    }
}
